package com.onlinemarket.thrid.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.onlinemarket.thrid.daoimpl.warehouseDaoImpl;
import com.onlinemarket.thrid.domain.WareHouse;

@Controller
@RequestMapping(value = "/warehouse")
public class warehouseControl {

	@Autowired
	private warehouseDaoImpl warehousedaoimpl;
	ModelAndView modelAndView = new ModelAndView();
	private WareHouse warehouse;

	@RequestMapping(value = "/toaddwarehouse")
	public String toaddwarehouse() {
		return "warehouse/addwarehouse";
	}

	@RequestMapping(value = "/getallwarehouse")
	public ModelAndView getallwarehouse() {
		List<WareHouse> warehouse = warehousedaoimpl.getAllwarehouse();
		modelAndView.addObject("warehouse", warehouse);
		modelAndView.setViewName("warehouse/viewallwarehouse");
		return modelAndView;
	}

	/**
	 * 通过id得到仓库相关信息
	 * 
	 * @return
	 */
	@RequestMapping(value = "/getwarehousebyid")
	public ModelAndView getwarehousebyid(int warehouse_id) {
		warehouse = warehousedaoimpl.getwarehouseById(warehouse_id);
		modelAndView.addObject("warehouse", warehouse);
		modelAndView.setViewName("warehouse/warehouseinfo");
		return modelAndView;
	}

	/**
	 * 通过id删除仓库信息
	 * 
	 * @param warehouse_id
	 * @return
	 */
	@RequestMapping(value = "/deletewarehouse")
	public ModelAndView deletewarehouse(int warehouse_id) {
		boolean isdelete = warehousedaoimpl.deletewarehouse(warehouse_id);
		if (isdelete) {
			System.out.println("删除成功！");
		} else {
			System.out.println("删除失败!");
		}
		return modelAndView;
	}

	/**
	 * 修改仓库信息
	 */
	@RequestMapping(value = "/updatewarehouseinfox")
	public void updatewarehouseinfo(@RequestParam("id") int id, @RequestParam("name") String name,
			@RequestParam("add") String add) {
		boolean isupdate = warehousedaoimpl.updateinfo(id, name, add);
		if (isupdate) {
			System.out.println("修改成功");
		} else {
			System.out.println("修改失败");
		}
	}

	/**
	 * 添加仓库成功
	 */
	@RequestMapping(value = "/addwarehouse")
	public void addwarehouse(@RequestParam("name") String name, @RequestParam("add") String add) {
		WareHouse w = new WareHouse();
		w.setWarehouse_name(name);
		w.setWarehouse_address(add);
		int insertnumber = warehousedaoimpl.addwarehouse(w);
		if (insertnumber == 1) {
			System.out.println("添加成功");
		} else {
			System.out.println("添加失败");
		}
	}
}
